
async function  postBrowseCount(db,post_id){
    if(!(db.query && post_id))
        return new Error('传递参数错误')
    let [res] = await db.query('SELECT COUNT(id) as number FROM `t_post_browse` WHERE post_id = ?',post_id)
    await db.query('UPDATE t_post SET browse = ? WHERE id = ?',[res[0].number,post_id])
}

async function saveContentImmgs(db,content){
     // 正则拿到content的img链接
     let imgList = [];   
     let reg = /<img.*?(?:>|\/>)/gi;
     let arr = content.match(reg);
     if(arr){
         arr.forEach(item => {
             let reg = /src="(.*?)"/gi;
             let arr = item.match(reg);
             if(arr){
                 imgList.push(arr[0].replace(/src="/g,'').replace(/"/g,''))
             }
         }
         )
         var upload =await save(ctx,{names:JSON.stringify(imgList),dir:'/images/post'})
         if(upload.code!=1000)
             return ctx.body = upload
         imgList.forEach((item,index) => {
             let newURL = ctx.config.host + upload.data.path[index]
             content = content.replace(item,newURL)
             imgList[index] = newURL
         })
         return imgList
     }
}

module.exports =  {
    //处理帖子浏览量
    postBrowseCount,
    // 保存帖子内容里的临时图片
    saveContentImmgs
}